<%@ include file="common.jsp"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
body {
	overflow-x: hidden;
	overflow-y: hidden;
	padding: 15px 15px 0 0;
}
</style>
</head>
<body>
	<form class="form-horizontal">
		<div class="form-group">
			<label class="col-sm-2 control-label">卡号：</label>
			<div class="col-sm-10">
				<input type="text" class="form-control" name="cid">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label">姓名：</label>
			<div class="col-sm-4">
				<input type="text" class="form-control" name="name">
			</div>
			<label class="col-sm-2 control-label">电话：</label>
			<div class="col-sm-4">
				<input type="text" class="form-control" name="number">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label">卡类型：</label>
			<div class="col-sm-4">
				<select class="form-control" name="type">
					<option value="1">虚拟卡</option>
					<option value="2">实体卡</option>
				</select>
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-1 col-sm-offset-2">
				<button class="btn btn-default" type="button" onclick="save()">保存</button>
			</div>
			<div class="col-sm-1">
				<button class="btn btn-default" type="button" onclick="closeLayer()">返回</button>
			</div>
		</div>
	</form>
</body>
<script type="text/javascript">
	var card;
	var $cid = $('input[name="cid"]');
	var $name = $('input[name="name"]');
	var $number = $('input[name="number"]');
	var $type = $('select[name="type"]');

	$(function() {
		if (parent.model == 1) {
			card = parent.map[parent.id];
			$cid.val(card.cid);
			$name.val(card.name);
			$number.val(card.number);
			$type.val(card.type);
		}
	});

	function save() {
		if (parent.model == 1) {
			update();
		} else {
			add();
		}
	}

	function add() {
		if (validate()) {
			var url = BaseUrl + 'membershipCardAction/registerCard.action';
			var option = {};
			option['cid'] = $cid.val();
			option['name'] = $name.val();
			option['number'] = $number.val();
			option['type'] = $type.val();

			$.a(url, option, function(res) {
				console.log(res);
				if (res.result) {
					parent.load();
					parent.layer.msg('保存成功', {
						time : 2000
					});
					closeLayer();
				} else {
					parent.layer.msg(res.message, {
						time : 2000
					});
				}
			}, false);
		}
	}

	function update() {
		if (validate()) {
			var url = BaseUrl + 'membershipCardAction/updateCardById.action';
			var option = {};
			option['id'] = card.id;
			option['cid'] = $cid.val();
			option['name'] = $name.val();
			option['number'] = $number.val();
			option['type'] = $type.val();
			$.a(url, option, function(res) {
				if (res.result) {
					parent.load();
					parent.layer.msg('保存成功', {
						time : 2000
					});
					closeLayer();
				} else {
					parent.layer.msg(res.message, {
						time : 2000
					});
				}
			}, false);
		}
	}

	function validate() {
		if (isEmpty($cid.val())) {
			layer.msg('卡号不能为空', {time : 2000});
			$cid.focus();
			return false;
		}
		if (isEmpty($name.val())) {
			layer.msg('姓名不能为空', {time : 2000});
			$name.focus();
			return false;
		}
		if (isEmpty($number.val())) {
			layer.msg('电话不能为空', {time : 2000});
			$number.focus();
			return false;
		}
		return true;
	}

	function closeLayer() {
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	}
</script>
</html>